package com.kkb.controller;

import com.kkb.domain.AjaxResult;
import com.kkb.pojo.SkGoods;
import com.kkb.pojo.SkGoodsExample;
import com.kkb.service.SkGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.kkb.domain.AjaxResult.error;

/**
 * 商品列表
 */
@Controller
@RequestMapping("/skgoods")
public class SkGoodsController {

    @Autowired
    private SkGoodsService skGoodsService;

    @RequestMapping("/goods")
    public String listgoods() {
        return "goods/goods";
    }

    /**
     * 查询所有
     * @return
     */
    @ResponseBody
    @RequestMapping("/skGoodsList")
    public List<SkGoods> selectByExample(SkGoodsExample example) {
        List<SkGoods> skGoods = skGoodsService.selectByExample();
        return skGoods;
    }


    @RequestMapping("/add")
    public String insertgoods() { return "goods/add"; }
    /**
     * 普通商品添加
     * @param record
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    public AjaxResult insert(SkGoods record){
        AjaxResult insert = skGoodsService.insert(record);
       return insert;
    }

    @RequestMapping("/editgoods")
    public String editgoods() { return "goods/edit"; }
    /**
     * 修改
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/edit")
    public AjaxResult edit(@Validated SkGoods record) {
        AjaxResult skGoods = skGoodsService.updateById(record);
        return skGoods;
    }
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        SkGoods skGoods = skGoodsService.selectGoodsById(recordId);
        mmap.put("skGoods", skGoods);
        return "goods/edit";
    }


    /**
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long goodsId) {
        AjaxResult deletegoodsById = skGoodsService.deletegoodsById(goodsId);
        return deletegoodsById;
    }
}
